﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using PgBirdie.Core.Config;
using PgBirdie.Core.Helpers;

namespace PgBirdie.Core.Service.Infrastructure
{
    public class FileSystemService : IFileSystemService
    {
        public IList<string> GetDirectoryNames(string path)
        {
            if (!Directory.Exists(path))
            {
                AppConsole.WriteLine("Could find {0}.", path);
                return null;
            }

            return Directory.GetDirectories(path);
        }

        public IList<string> GetFilenames(string path)
        {
            if (!Directory.Exists(path))
            {
                AppConsole.WriteLine("Could find {0}.", path);
                return null;
            }

            return Directory.GetFiles(path);
        }

        public IList<string> GetLastSegmentsFromPaths(IEnumerable<string> paths)
        {
            var segments = new List<string>();
            foreach (var path in paths)
            {
                var segs = path.Split(Settings.SplitterFor.Backslash, StringSplitOptions.RemoveEmptyEntries);
                segments.Add(segs[segs.Length - 1]);
            }

            return segments;
        }

        public bool CreateFile(string path, string text, bool withBom)
        {
            var directoryPath = Path.GetDirectoryName(path);
            if (directoryPath == null)
                throw new DirectoryNotFoundException("Directory path should never be null at this point.");

            if (!Directory.Exists(directoryPath))
                Directory.CreateDirectory(directoryPath);

            //if (!File.Exists(path))
            //    File.Create(path);)

            using (var writer = new StreamWriter(path, false, new UTF8Encoding(withBom)))
            {
                try
                {
                    writer.Write(text);
                    return true;
                }
                catch (DirectoryNotFoundException)
                {
                    AppConsole.WriteLine("Could not find path {0}.", path);
                    return false;
                }
                catch (UnauthorizedAccessException)
                {
                    AppConsole.WriteLine("Unauthorized access to {0}.", path);
                    return false;
                }
            }
        }

        public string ReadFile(string path)
        {
            return File.ReadAllText(path, Encoding.UTF8);
        }

        public void DeleteDirectory(string path)
        {
            string[] directories;
            try
            {
                directories = Directory.GetDirectories(path);
            }
            catch (UnauthorizedAccessException ex)
            {
                AppConsole.WriteLine(ex.ToString());
                return;
            }

            var files = Directory.GetFiles(path);

            foreach (var file in files)
            {
                File.SetAttributes(file, FileAttributes.Normal);
                File.Delete(file);
            }

            foreach (string directory in directories)
                DeleteDirectory(directory);

            Directory.Delete(path, false);
        }

        public void DeleteFile(string path)
        {
            if (File.Exists(path))
                File.Delete(path);
        }
    }
}